package cz.zcu.fav.ratdroid.activity.calendar.reservation;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.Duration;
import org.joda.time.LocalDateTime;

import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.bean.Object;
import cz.zcu.fav.ratdroid.bean.PickAction;
import cz.zcu.fav.ratdroid.bean.Reservation;
import cz.zcu.fav.ratdroid.bean.Reservations;
import cz.zcu.fav.ratdroid.bean.ReturnAction;
import cz.zcu.fav.ratdroid.bean.User;
import cz.zcu.fav.ratdroid.activity.MenuActivity;
import cz.zcu.fav.ratdroid.activity.calendar.day.DayActivity;
import cz.zcu.fav.ratdroid.bean.Period;
import cz.zcu.fav.ratdroid.service.RestService;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CreateReservationActivity extends AbstractReservationActivity
{
	private LocalDateTime selectedDateTime;
	private Object object;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		selectedDateTime = (LocalDateTime) getIntent().getSerializableExtra(
				DayActivity.PRESELECTED_DATETIME);
		object = (Object) getIntent().getSerializableExtra(
				Object.SINGLE_OBJECT_IDENTIFIER);
		super.onCreate(savedInstanceState);
	}

	public Object getObject()
	{
		return object;
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.reservation_submit_menu, menu);
		return true;
	}

	public Period loadBackgroudData()
	{
		Reservation reservation = new Reservation();
		// at least one is always set
		reservation.setPeriodCount(1);
		reservation.setPeriodical(false);
		reservation.setObjectName(object.getName());
		String username = getSharedPreferences(User.SINGLE_USER_IDENTIFIER,
				MODE_PRIVATE).getString(User.USER_NAME, "");

		reservation.setObjectId(object.getId());
		reservation.setUserName(username);

		List<Period> periods = new ArrayList<Period>();
		Period period = new Period();

		period.setFromDate(selectedDateTime);
		
		Duration minDuration = org.joda.time.Duration
				.standardMinutes(getSharedPreferences(
						MenuActivity.CALENDAR_GENERAL_PREFERENCES, MODE_PRIVATE)
						.getLong(MenuActivity.MINIMUM_PERIOD_DURATION, 0));
		
		period.setToDate(selectedDateTime.plus(minDuration));
		period.setReservation(reservation);
		period.setValid(true);

		period.setPickAction(object.isPickable() ? PickAction.NOT_PICKED
				: PickAction.NOT_PICKABLE);
		period.setReturnAction(object.isReturnable() ? ReturnAction.NOT_RETURNED
				: ReturnAction.NOT_RETURNABLE);

		periods.add(period);
		reservation.setPeriods(periods);

		return period;
	}

	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.reservationSubmitButton:
			if (validateForm())
			{
				Reservations reservations = ReservationFactory
						.createReservations(
								getModifiedPeriod().getFromDate(),
								getModifiedPeriod().getToDate(),
								getModifiedPeriod().getReservation()
										.getPeriodCount(),
								object,
								getSharedPreferences(
										User.SINGLE_USER_IDENTIFIER,
										MODE_PRIVATE).getInt(User.USER_ID, 0),
								getModifiedPeriod().isValid(),
								getModifiedPeriod().getPickAction());
				setRestTask(RestService.getInstance().addReservation(this, this,
						reservations));
			}
			break;
		default:
			return true;
		}
		return true;
	}

}
